package com.shigu.mo.actions;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

@Controller
public class CallbackController {
    private static final Logger logger = LoggerFactory.getLogger(CallbackController.class);
    /**
     * 回调用的测试
     * @return
     */
    @RequestMapping("callbacktest")
    @ResponseBody
    public String callbacktest(HttpServletRequest request){
        Enumeration em = request.getParameterNames();
        JSONObject obj=new JSONObject();
        while (em.hasMoreElements()) {
            String name = (String) em.nextElement();
            String value = request.getParameter(name);
            obj.put(name,value);
        }
        logger.error(obj.toJSONString());
        return "success";
    }

    /**
     * 回调用的测试
     * @return
     */
    @RequestMapping("callbackpayloadtest")
    @ResponseBody
    public String callbackpayloadtest( HttpServletRequest request){
        StringBuilder sb = new StringBuilder();
        try(BufferedReader reader = request.getReader()) {
            char[]buff = new char[1024];
            int len;
            while((len = reader.read(buff)) != -1) {
                sb.append(buff,0, len);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        logger.error(sb.toString());
        return "success";
    }
}
